package com.shixi.shixitraining.shixitraining.ui.tabhome.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shixi.shixitraining.shixitraining.R;
import com.shixi.shixitraining.shixitraining.base.BaseActivity;
import com.shixi.shixitraining.shixitraining.http.CallbackImple;
import com.shixi.shixitraining.shixitraining.http.HttpUtil;
import com.shixi.shixitraining.shixitraining.test.TestPopup;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.OverseasListAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.adapter.PopupEmploymentAdapter;
import com.shixi.shixitraining.shixitraining.ui.tabhome.entity.HotJobBean;
import com.shixi.shixitraining.shixitraining.util.DensityUtil;
import com.shixi.shixitraining.shixitraining.util.TitleUtil;
import com.shixi.shixitraining.shixitraining.util.ToastUtil;
import com.shixi.shixitraining.shixitraining.view.DropDownMenu;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleDivider;
import com.shixi.shixitraining.shixitraining.view.recycleview.RecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

import static com.shixi.shixitraining.shixitraining.constant.Constants.HOST;

public class OverseasEmploymentActivity extends BaseActivity implements View.OnClickListener {

    private TabLayout mTab;
    private String tabs[] = {"综合排序", "实训项目", "国家"};
    DropDownMenu dropDownMenu;
    private RecyclerView recyclerView;
    private SmartRefreshLayout refreshLayout;
    private TextView tvNodata;
    private List<View> popups = new ArrayList<>();
    private PopupWindow popupWindow;
    private TextView mCompositeTv;
    private float alpha = 1.0f;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case 1:
                    backgroundAlpha((Float) message.obj);
                    break;
            }
            return false;
        }
    });
    private RelativeLayout relativeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_overseas_employment);
        initTitle();
//      initPopup();
        initData();

    }

    private void initData() {
        HttpUtil.getInstance().getApiInterface().hotJob(HOST+"/job/index").enqueue(new CallbackImple<HotJobBean>() {
            @Override
            public void onError(Call<HotJobBean> call, Throwable t) {
                ToastUtil.showShortToast(OverseasEmploymentActivity.this,t.getMessage());
            }

            @Override
            public void onSuccess(Call<HotJobBean> call, HotJobBean response) {
                if (response!=null&&response.getData()!=null){
                    HotJobBean.DataBean JobData = response.getData();
                    final List<HotJobBean.DataBean.ListsBean>  jobListData = JobData.getLists();

                    RecyclerView mRcView = (RecyclerView) findViewById(R.id.rcview_tp);
                    LinearLayoutManager manager = new LinearLayoutManager(OverseasEmploymentActivity.this);
                    RecycleDivider divider = new RecycleDivider(new ColorDrawable(ContextCompat.getColor(OverseasEmploymentActivity.this, R.color.grayc5)),
                            OrientationHelper.VERTICAL);
                    divider.setHeight(OverseasEmploymentActivity.this, 0.5f);
                    mRcView.addItemDecoration(divider);
                    manager.setOrientation(LinearLayoutManager.VERTICAL);
                    mRcView.setLayoutManager(manager);

                    OverseasListAdapter adapter = new OverseasListAdapter(OverseasEmploymentActivity.this,jobListData);
                    mRcView.setAdapter(adapter);
                    mRcView.addOnItemTouchListener(new RecycleItemClickListener(OverseasEmploymentActivity.this) {
                        @Override
                        public void itemClickListener(View view, int position) {
                            HotJobBean.DataBean.ListsBean listsBean = jobListData.get(position);
                            String jobId = listsBean.getId();
                            Bundle bundle=new Bundle();
                            bundle.putString("jobId",jobId);
                            Intent intent=new Intent(OverseasEmploymentActivity.this,OverseasDetailActivity.class);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                }else{
                    ToastUtil.showShortToast(OverseasEmploymentActivity.this,"数据为空");
                }




            }
        });



    }

    private void initPopup() {
        //综合排序

//        View  mComposite = getLayoutInflater().inflate(R.layout.item_composite, null);
//        RecyclerView mPopup = (RecyclerView) mComposite.findViewById(R.id.poppup_rcview);
//        LinearLayoutManager manager1 = new LinearLayoutManager(this);
//        mPopup.setLayoutManager(manager1);
//        List<TestPopup> data1 = new ArrayList<>();
//        data1.add(new TestPopup("综合排序"));
//        data1.add(new TestPopup("项目时间"));
//        data1.add(new TestPopup("价格降序"));
//        data1.add(new TestPopup("价格升序"));
//        PopupAdapter adapter1 = new PopupAdapter(this, data1);
//        mPopup.setAdapter(adapter1);


        /*_____________________实训项目______________________________*/

//        initTraining();

//        View     mTraining = getLayoutInflater().inflate(R.layout.item_composite, null);
//        RecyclerView mPopup2= (RecyclerView) mTraining.findViewById(R.id.poppup_rcview);
//        LinearLayoutManager manager2 = new LinearLayoutManager(this);
//        mPopup2.setLayoutManager(manager2);
//
//        List<TestPopup> data2 = new ArrayList<>();
//        data2.add(new TestPopup("综合排序"));
//        data2.add(new TestPopup("项目时间"));
//        data2.add(new TestPopup("价格降序"));
//        data2.add(new TestPopup("价格升序"));
//        PopupAdapter adapter2 = new PopupAdapter(this, data2);
//        mPopup2.setAdapter(adapter2);

        /*---------------- 国家-------------------------*/
//        initGroup();
//      View  mGroup = getLayoutInflater().inflate(R.layout.item_composite, null);
//        RecyclerView mPopup3 = (RecyclerView) mComposite.findViewById(R.id.poppup_rcview);
//        LinearLayoutManager manager3 = new LinearLayoutManager(this);
//        mPopup3.setLayoutManager(manager3);
//        List<TestPopup> data3 = new ArrayList<>();
//        data3.add(new TestPopup("综合排序"));
//        data3.add(new TestPopup("项目时间"));
//        data3.add(new TestPopup("价格降序"));
//        data3.add(new TestPopup("价格升序"));
//
//        PopupAdapter adapter = new PopupAdapter(this, data3);
//        mPopup3.setAdapter(adapter);
//        mPopup3.addOnItemTouchListener(new RecycleItemClickListener(this) {
//            @Override
//            public void itemClickListener(View view, int position) {
//
//            }
//        });
//
//
//        View content = getLayoutInflater().inflate(R.layout.joblist_content, null);
//
//        refreshLayout = (SmartRefreshLayout) content.findViewById(R.id.refreshLayout);
//        recyclerView = (RecyclerView) content.findViewById(R.id.recview_jobs);
//        LinearLayoutManager manager = new LinearLayoutManager(this);
//        recyclerView.setLayoutManager(manager);
//        tvNodata = (TextView) content.findViewById(R.id.tv_nodata);
//        popups.add(mComposite);
//        popups.add(mTraining);
//        popups.add(mGroup);
//        dropDownMenu.setDropDownMenu(Arrays.asList(tabs), popups, content);

    }

    private void initTitle() {

        TitleUtil titleUtil = new TitleUtil(findViewById(R.id.top_bar_layout));
        titleUtil.initTitle(4, R.id.top_bar_center_tv, R.id.top_bar_left_img, 0,
                R.drawable.icon_back, 0, "海外就业", "", "");

        ImageView mBackImg = (ImageView) findViewById(R.id.top_bar_left_img);
        mBackImg.setOnClickListener(this);
        relativeLayout = (RelativeLayout) findViewById(R.id.rl);
        mCompositeTv = (TextView) findViewById(R.id.tv_compiste);
        TextView mTrainingTv = (TextView) findViewById(R.id.tv_training);
        TextView mGroupTv = (TextView) findViewById(R.id.tv_group);
        mCompositeTv.setOnClickListener(this);
        mTrainingTv.setOnClickListener(this);
        mGroupTv.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_bar_left_img:
                this.finish();
                break;
            case R.id.tv_compiste:
                initCompositePopup();
                delayPopup();
                break;
        }
    }



    private void initCompositePopup() {
        View  mContentView = getLayoutInflater().inflate(R.layout.item_composite, null);
        if (popupWindow==null){
            int screenHeightPixels = DensityUtil.getScreenHeightPixels(this);
            int screenWidthPixels = DensityUtil.getScreenWidthPixels(this);
            popupWindow = new PopupWindow(mContentView,screenWidthPixels,screenHeightPixels/2);
            popupWindow.setBackgroundDrawable(new ColorDrawable(0x0000000));
        }

        popupWindow.setFocusable(true);
        popupWindow.setContentView(mContentView);
        popupWindow.showAsDropDown(mCompositeTv);
        RecyclerView mPopup = (RecyclerView) mContentView.findViewById(R.id.rcview_poppup_composite);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        RecycleDivider divider=new RecycleDivider(new ColorDrawable( ContextCompat.getColor(this,R.color.gray)),OrientationHelper.VERTICAL);
        divider.setHeight(this,0.5f);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mPopup.setLayoutManager(manager);
        mPopup.addItemDecoration(divider);
        List<TestPopup> data = new ArrayList<>();
        data.add(new TestPopup("不限"));
        data.add(new TestPopup("会计服务"));
        data.add(new TestPopup("航空公司"));
        data.add(new TestPopup("银行"));
        data.add(new TestPopup("旅游"));
        data.add(new TestPopup("电子商务"));
        data.add(new TestPopup("表演艺术"));
        data.add(new TestPopup("翻译和本地化"));
        data.add(new TestPopup("服装和时装"));
        PopupEmploymentAdapter adapter = new PopupEmploymentAdapter(this, data);
        mPopup.setAdapter(adapter);
        mPopup.addOnItemTouchListener(new RecycleItemClickListener(this) {
            @Override
            public void itemClickListener(View view, int position) {
                popupWindow.dismiss();
            }
        });

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        while (alpha < 1.0f) {
                            Message message = handler.obtainMessage();
                            message.what = 1;
                            alpha += 0.01f;
                            message.obj = alpha;
                            handler.sendMessage(message);
                        }
                    }
                }).start();
            }
        });

        backgroundAlpha(1);
    }

    private void delayPopup() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (alpha > 0.5f) {
                    Message message = handler.obtainMessage();
                    message.what = 1;
                    alpha -= 0.01f;
                    message.obj = alpha;
                    handler.sendMessage(message);
                }
            }
        }).start();
    }

    //设置popupwindown的背景颜色
    private void backgroundAlpha(float bgAlpha) {

        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    }
}
